"""
浏览器代理工具 - Web管理界面
重构后的主应用文件，使用模块化架构
"""
import os
import re
import time
import subprocess
import psutil
from datetime import datetime
from pathlib import Path
from flask import Flask, request, jsonify, render_template, send_file
from flask_cors import CORS
from flask_socketio import SocketIO
import yaml

# 导入模块化的管理器
from web_manager import (
    ConfigManager,
    ProxyProcessManager,
    SystemProxyManager,
    LogManager,
    register_main_routes,
    register_api_routes
)

# 创建Flask应用
app = Flask(__name__, static_folder='templates/static', static_url_path='/static')
CORS(app)
socketio = SocketIO(app, cors_allowed_origins="*")

# 初始化管理器实例
config_manager = ConfigManager()
proxy_process_manager = ProxyProcessManager()
system_proxy_manager = SystemProxyManager()
log_manager = LogManager()

# 注册路由
register_main_routes(app)
register_api_routes(app, config_manager, proxy_process_manager, system_proxy_manager, log_manager)

if __name__ == '__main__':
    # 从配置文件读取Web服务器配置
    web_config = config_manager.get_web_config()
    host = web_config.get('host', '0.0.0.0')
    port = web_config.get('port', 5001)
    
    print(f"启动Web管理界面: http://{host}:{port}")
    print(f"配置文件: {config_manager.config_path}")
    
    socketio.run(app, host=host, port=port, debug=True)